﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity.Car_Rental;
using System.Timers;
namespace Data.Car_Rental
{
    public class HelperCenterCategoryRepository : Car_RentalRepositoryBase, IHelperCenterCategoryRepository
    {
        #region IRepository<HelperCenterCategory> 成员

        public void Update(Entity.Car_Rental.HelperCenterCategory entity)
        {
            base.UpdateEntity(entity);
        }

        public void Update(IList<Entity.Car_Rental.HelperCenterCategory> list)
        {
            base.UpdateEntity(list.Cast<Entity.EntityBase>().ToList());
        }

        public void Insert(Entity.Car_Rental.HelperCenterCategory entity)
        {
            base.InsertEntity(entity);
        }

        public void Insert(IList<Entity.Car_Rental.HelperCenterCategory> list)
        {
            base.InsertEntity(list.Cast<Entity.EntityBase>().ToList());
        }

        public Entity.Car_Rental.HelperCenterCategory InsertGetIDENTITY(Entity.Car_Rental.HelperCenterCategory entity)
        {
            base.InsertEntity(entity);
            return this.Find(entity.PrimaryKey);
        }

        public void Delete(Entity.Car_Rental.HelperCenterCategory entity)
        {
            base.DeleteEntity(entity);
        }

        public void Delete(IList<Entity.Car_Rental.HelperCenterCategory> list)
        {
            base.DeleteEntity(list.Cast<Entity.EntityBase>().ToList());
        }

        public IQueryable<Entity.Car_Rental.HelperCenterCategory> GetModel()
        {
            return base.GetEntities<HelperCenterCategory>().OrderBy(i => i.SortNumber);
        }

        public Entity.Car_Rental.HelperCenterCategory Find(params object[] keyValues)
        {
            return base.Find<HelperCenterCategory>(keyValues);
        }

        #endregion

        #region 分类缓存
        static Timer sysTimer = new Timer(1);
        /// <summary>
        /// 数据实体
        /// </summary>
        public static volatile List<HelperCenterCategory> Instance = null;
        // Lock对象，线程安全所用
        private static object syncRoot = new Object();
        /// <summary>
        /// 类构造方法
        /// </summary>
        static HelperCenterCategoryRepository()
        {
            ReLoad();
            sysTimer.AutoReset = true;
            sysTimer.Enabled = true;
            sysTimer.Elapsed += new ElapsedEventHandler(sysTimer_Elapsed);
            sysTimer.Start();
        }
        static void sysTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            ReLoad();
        }
        /// <summary>
        /// 重新加载数据结果集
        /// </summary>
        /// <returns></returns>
        public static List<HelperCenterCategory> ReLoad()
        {
            lock (syncRoot)
            {
                Instance = new HelperCenterCategoryRepository().GetModel().ToList();
            }
            return Instance;
        }
        #endregion

        #region 得到完整树
        /// <summary>
        /// 得到指定ＩＤ下面的完整树结构
        /// </summary>
        /// <param name="helpCategoryID"></param>
        /// <returns></returns>
        public HelperCenterCategory GetHelperCenterCategoryTree(int helpCategoryID)
        {
            HelperCenterCategory helperCenterCategory = new HelperCenterCategory { HelpCategoryID = helpCategoryID };
            helperCenterCategory.Sons = GetHelperCenterCategoryTree(helperCenterCategory);
            return helperCenterCategory;
        }
        public HelperCenterCategory GetHelperCenterCategoryTree()
        {
            return GetHelperCenterCategoryTree(0);
        }
        public List<HelperCenterCategory> GetHelperCenterCategoryTree(HelperCenterCategory helperCenterCategory)
        {
            return (new HelperCenterCategoryRepository().GetModel()
                .Where(i => i.ParentID == helperCenterCategory.HelpCategoryID)
                .Select(item => new HelperCenterCategory_Ext
                                 {
                                     HelpCategoryID = item.HelpCategoryID,
                                     Name = item.Name,
                                     ParentID = item.ParentID,
                                     SortNumber = item.SortNumber,
                                     Sons = GetHelperCenterCategoryTree(item)
                                 })).Cast<HelperCenterCategory>().OrderBy(i => i.SortNumber).ToList();
        }
        #endregion
    }
}
